﻿using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;

namespace Xms.Abp.Storage.DependencyInjection;

public class XmsStorageRepositoryRegistrar : EfCoreRepositoryRegistrar
{
    public XmsStorageRepositoryRegistrar(AbpDbContextRegistrationOptions options)
        : base(options)
    {

    }

    protected override Type GetRepositoryType(Type dbContextType, Type entityType)
    {
        return typeof(XmsStorageRepository<,>).MakeGenericType(dbContextType, entityType);
    }

    protected override Type GetRepositoryType(Type dbContextType, Type entityType, Type primaryKeyType)
    {
        return typeof(XmsStorageRepository<,,>).MakeGenericType(dbContextType, entityType, primaryKeyType);
    }

    protected override void RegisterCustomRepositories()
    {
        foreach (var customRepository in Options.CustomRepositories)
        {
            Options.Services.AddDefaultXmsRepository(customRepository.Key, customRepository.Value, replaceExisting: true);
        }
    }

    protected override void RegisterDefaultRepository(Type entityType)
    {
        Options.Services.AddDefaultXmsRepository(
            entityType,
            GetDefaultRepositoryImplementationType(entityType)
        );
    }
}
